package com.tairan.chapter.flowable.service.ext.impl;

import com.google.common.collect.Lists;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.idm.api.Group;
import org.flowable.idm.engine.impl.GroupQueryImpl;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntity;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;

import java.util.ArrayList;
import java.util.List;

public class FlowGroupQueryImpl extends GroupQueryImpl {

    private static final long serialVersionUID = -2665747007024690881L;

    @Override
    public long executeCount(CommandContext commandContext) {
        return executeQuery().size();
    }

    @Override
    public List<Group> executeList(CommandContext commandContext) {
        return executeQuery();
    }

    protected List<Group> executeQuery() {
        if (null != getUserId()) {
            return findGroupsByUser(getUserId());
        } else {
            return findAllGroups();
        }
    }

    protected List<Group> findGroupsByUser(String userId) {
        List<Group> list = Lists.newArrayList();

        GroupEntity groupEntity = new GroupEntityImpl();
        if ("litairan".equals(userId)) {
            groupEntity.setId("dept");
            groupEntity.setName("部门经理");
            groupEntity.setType("security-role");
            groupEntity.setRevision(1);
        } else if ("zhanghao".equals(userId)) {
            groupEntity.setId("hr");
            groupEntity.setName("hr");
            groupEntity.setType("assignment");
            groupEntity.setRevision(1);
        }
        list.add(groupEntity);
        System.out.println(groupEntity.getId() + "---" + userId);
        return list;
    }

    protected List<Group> findAllGroups() {
        return Lists.newArrayList();
    }
}
